package model.data.tableModel.sale;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import enums.Status;
import model.data.interfaces.IWriteableToDB;
import model.data.tableModel.TableControl;
import model.data.tableModel.person.Staff;
import model.handler.databaseHandling.QueryFactory;

/**
 * Represents a salesday.
 * <p>
 * Representation of the table salesday stored in the database.
 * Implements the interface <code>IWriteble</code>.
 * 
 * version %I%, %G%
 * @see IWriteableToDB
 * @see Staff
 */
public final class Salesday implements IWriteableToDB{

	private final static Logger debug = LogManager.getLogger("DEBUG");
	
	private final int salesdayID;
	private final String date;
	private final Staff staff;
	private final int coinBegin;
	private final double cashBegin;
	
	private int coinEnd;
	private double cashEnd;
	private Status status;
	
	public Salesday(String date, Staff staff, Status status, int coinBegin, double cashBegin) {
		assert date != null && !"".equals(date);
		assert staff != null;
		assert coinBegin >= 0;
		assert cashBegin >= 0;
		
		this.salesdayID = TableControl.getCentralCounter();
		this.date = date;
		this.staff = staff;
		this.status = status;
		this.coinBegin = coinBegin;
		this.cashBegin = cashBegin;
		
		debug.debug("New salesday with ID: " + salesdayID + " created.");
	}

	@Override
	public boolean writeToDB() {
		return QueryFactory.writeSalesdayToDB(false, salesdayID, date, staff.getStaffID(), status, cashBegin, cashEnd, coinBegin, coinEnd);
	}
	
	@Override
	public boolean writeUpdateToDB() {
		return QueryFactory.writeSalesdayToDB(true, salesdayID, date, staff.getStaffID(), status, cashBegin, cashEnd, coinBegin, coinEnd);
	}
	
	@Override
	public String toString(){
		String str = date + " || " + staff.getNickname() + " || " + status + " || coinBegin: " + coinBegin + " || coinEnd: " + coinEnd + " || cashBegin: " + cashBegin + " || cashEnd: " + cashEnd;
		return str;
	}

	@Override
	public int hashCode() {

		final int prime = 31;
		int result = 1;
		result = prime * result + salesdayID;
		result = prime * result + coinBegin;
		result = prime * result + coinEnd;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + ((staff == null) ? 0 : staff.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {

		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Salesday other = (Salesday) obj;
		if (salesdayID != other.salesdayID)
			return false;
		if (coinBegin != other.coinBegin)
			return false;
		if (coinEnd != other.coinEnd)
			return false;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (staff == null) {
			if (other.staff != null)
				return false;
		} else if (!staff.equals(other.staff))
			return false;
		return true;
	}
	
	/* GETTER AND SETTER BELOW */	

	public Staff getStaff() {
		return staff;
	}

	public int getID() {
		return salesdayID;
	}
	
	public void setCoinEnd(int coinEnd){
		this.coinEnd = coinEnd;
	}
	
	public void setCashEnd(double numberCash){
		this.cashEnd = numberCash;
	}
}
